package net.wds.study.major.dao.impl;/**
 * Package: net.wds.study.major.dao.impl
 * Description：
 * Author: changxiaohu
 * Date: Created in 2020/9/16 10:19
 * Company:
 * Version: 0.0.1
 */

import net.wds.study.major.dao.MajorDao;
import net.wds.study.major.entity.Major;
import net.wds.study.student.modal.PageModal;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : MajorDaoImpl  
 * @Description :   
 * @Author : changxiaohu  
 * @Date: 2020-09-16 10:19  
 */
@Repository("majorDao")
public class MajorDaoImpl implements MajorDao {
    @Resource
    private SessionFactory sf;
    /*   获取当前Session*/
    public Session getSession(){  return sf.getCurrentSession();}

    public SessionFactory getSf(){  return sf;}

    @Override
    public Serializable addMajor(Major major) {
        Serializable save = getSession().save(major);
        System.out.println(save);
        return save!=null?true:false;
    }

    @Override
    public boolean deleteMajor(Integer id) {
        try{
        StringBuilder hql=new StringBuilder(" delete Major where id="+id);
            getSession().createQuery(hql.toString()).executeUpdate();
        }catch( Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public PageModal<Major> getPageQuery(Integer startIndex,Integer pageSize,String name) {
        PageModal<Major> pageModal=new PageModal<Major>();
        if(pageSize!=null&&pageSize>0){
            pageModal.setPageSize(pageSize);
        }
        List<Object> param=new ArrayList<>();
        StringBuilder hql=new StringBuilder("select m from Major m where 1=1 ");
        if(name!=""&&!("".equals(name.trim()))){
            System.out.println("满足条件");
            hql.append(" and m.majorName like ?  ");
            param.add("%"+name.trim()+"%");
        }

        Query query = getSession().createQuery(hql.toString());
        System.out.println(query);
        for(int i=0;i<param.size();i++){
            query.setParameter(i,param.get(i));
        }
        int totalPageNumber=query.list().size();//查询到的总条数
        pageModal.setTotal(totalPageNumber);

        //计算总页数
        int totalPageNum=totalPageNumber%pageModal.getPageSize()==0? totalPageNumber/pageModal.getPageSize():(totalPageNumber/pageModal.getPageSize()+1);
        pageModal.setTotalPageNum(totalPageNum);

        //添加分页信息
        query.setFirstResult(startIndex);
        query.setMaxResults(pageModal.getPageSize());

        List<Major> list=query.list();
        pageModal.setPageData(list);
        return pageModal;

    }

    @Override
    public boolean updateMajor(Major major) {
        StringBuilder hql=new StringBuilder(" update Major m set ");
        hql.append(" m.majorName="+"'"+major.getMajorName()+"',");
        hql.append(" m.majorCount="+major.getMajorCount()+",");
        hql.append(" m.majorYear="+major.getMajorYear()+",");
        hql.append(" m.majorScore="+major.getMajorScore()+" ");
        hql.append(" where m.id="+major.getId());
        Query query = getSession().createQuery(hql.toString());
        return query.executeUpdate()>0?true:false;
    }
}
